import Vue from 'vue'
import { loadingText, messageDuration } from '@/config'
import { escapeHtml } from '@/utils'

/**
 * @description 全局加载层
 * @param {*} index
 * @param {*} text
 */
Vue.prototype.$baseLoading = (index, text) => {
  let loading
  if (!index) {
    loading = Vue.prototype.$loading.service({
      lock: true,
      text: text || loadingText,
      background: 'hsla(0,0%,100%,.8)'
    })
  } else {
    loading = Vue.prototype.$loading.service({
      lock: true,
      text: text || loadingText,
      spinner: 'vab-loading-type' + index,
      background: 'hsla(0,0%,100%,.8)'
    })
  }
  return loading
}

/**
 * @description 全局多彩加载层
 * @param {*} index
 * @param {*} text
 */
Vue.prototype.$baseColorfullLoading = (index, text) => {
  let loading
  if (!index) {
    loading = Vue.prototype.$loading.service({
      lock: true,
      text: text || loadingText,
      spinner: 'dots-loader',
      background: 'hsla(0,0%,100%,.8)'
    })
  } else {
    switch (index) {
      case 1:
        index = 'dots'
        break
      case 2:
        index = 'gauge'
        break
      case 3:
        index = 'inner-circles'
        break
      case 4:
        index = 'plus'
        break
    }
    loading = Vue.prototype.$loading.service({
      lock: true,
      text: text || loadingText,
      spinner: index + '-loader',
      background: 'hsla(0,0%,100%,.8)'
    })
  }
  return loading
}

/**
 * @description 全局Message
 * @param {*} message
 * @param {*} type
 * @param {*} dangerouslyUseHTMLString
 * @param {*} customClass
 */
Vue.prototype.$baseMessage = (
  message,
  type,
  dangerouslyUseHTMLString,
  customClass
) => {
  Vue.prototype.$message({
    showClose: true,
    message,
    type,
    dangerouslyUseHTMLString,
    duration: messageDuration,
    customClass
  })
}

/**
 * @description 全局Alert
 * @author chuzhixin 1204505056@qq.com
 * @param {*} content
 * @param {*} title
 * @param {function} callback
 */
Vue.prototype.$baseAlert = (content, title, callback) => {
  Vue.prototype.$alert(content, title || '温馨提示', {
    confirmButtonText: '确定',
    dangerouslyUseHTMLString: true,
    callback: () => {
      if (callback) {
        callback()
      }
    }
  }).then(() => {})
}

/**
 * @description 全局Confirm
 * @param {*} content
 * @param {*} title
 * @param {*} callback1
 * @param {*} callback2
 * @param {*} confirmButtonText
 * @param {*} cancelButtonText
 */
Vue.prototype.$baseConfirm = (
  content,
  title,
  callback1,
  callback2,
  confirmButtonText,
  cancelButtonText
) => {
  Vue.prototype.$confirm(content, title || '温馨提示', {
    confirmButtonText: confirmButtonText || '确定',
    cancelButtonText: cancelButtonText || '取消',
    closeOnClickModal: false,
    type: 'danger',
    confirmButtonClass: 'el-button--danger',
    lockScroll: false,
    dangerouslyUseHTMLString: true
  })
    .then(() => {
      if (callback1) {
        callback1()
      }
    })
    .catch(() => {
      if (callback2) {
        callback2()
      }
    })
}

/**
 * @description 全局Notification
 * @param {*} message
 * @param {*} title
 * @param {*} type
 * @param {*} position
 */
Vue.prototype.$baseNotify = (message, title, type, position) => {
  Vue.prototype.$notify({
    title: title,
    message: message,
    position: position || 'top-right',
    type: type || 'success',
    duration: messageDuration
  })
}

/**
 * @description 表格高度
 * @param {*} formType
 */
Vue.prototype.$baseTableHeight = (formType) => {
  let height = window.innerHeight
  const paddingHeight = 291
  const formHeight = 60

  if (typeof formType === 'number') {
    height = height - paddingHeight - formHeight * formType
  } else {
    height = height - paddingHeight
  }
  return height
}

Vue.prototype.$baseConfirmDelete = (
  content,
  title,
  callback1,
  callback2
) => {
  Vue.prototype.$baseConfirm(
    `<div class="el-message-box-delete">
      <i class="el-icon-error"></i>
      <div class="title">
        <div>是否删除${escapeHtml(content)} ？</div>
        <div class="tip">删除后数据将无法恢复，请谨慎操作</div>
      </div>
    </div>`,
    title,
    callback1,
    callback2
  )
}
